/*	Author		: Arga
 *	Created On	: Apr 30, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import team8.paacdroid.R;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ViewFavoritesActivity extends RootActivity {
	// UI declarations
	ListView lv;
	TextView tvFavoritesTitle;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.view_favorite_activity);
	    lv = (ListView) findViewById(R.id.listview);
	    tvFavoritesTitle = (TextView) findViewById(R.id.tvFavoritesTitle);
	    
	    // Put the header
	    tvFavoritesTitle.setText(getResources().getText(R.string.labelViewFavoritesActivity));	    

	    // create the grid item mapping
	    String[] from = new String[] {"origin", "destination"};
	    int[] to = new int[] { R.id.tvOrigin, R.id.tvDestination };

	    // prepare the list of all records
	    List<HashMap<String, String>> fillMaps = getAllFavorites();

	    // fill in the grid_item layout
	    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.favorite_item, from, to);
	    lv.setAdapter(adapter);
	}
	
	public ArrayList<HashMap<String, String>> getAllFavorites() {
		ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
		
		// Open the favorites file
		InputStream in;
		ArrayList<String> lines = new ArrayList<String>();
		try {
			in = openFileInput(getResources().getText(R.string.favorites_file_name).toString());
			if (in != null) {
				InputStreamReader tmp = new InputStreamReader(in);
				BufferedReader reader = new BufferedReader(tmp);
				String str;
				
				// Grab each line
				while ((str = reader.readLine()) != null) {
					lines.add(str);
				}
				
				in.close();
			}
		} catch (FileNotFoundException e) {
			Log.e(getString(R.string.app_name), "FileNotFoundException: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("An error had occured: IOException.");
			AlertDialog alert = builder.create();
			alert.show();
			Log.e(getString(R.string.app_name), "IOException: " + e.getMessage());
			e.printStackTrace();
		}
		
		// Put the favorites into a hashmap and eventually, into a string
		for (String line : lines) {
			String[] foo = line.split("///");
			HashMap<String, String> bar = new HashMap<String, String>();
			bar.put("origin", foo[0]);
			bar.put("destination", foo[1]);
			result.add(bar);
		}
		
		// Return result
		return result;
	}
}
